﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Rabbit.Model;
using Rabbit.BusinessLogicLayer;

namespace MicroBlogClient.Pages
{
    /// <summary>
    /// Interaction logic for Search.xaml
    /// </summary>
    public partial class Search : PageBase
    {
        ObservableCollection<Status> StatusCollection = new ObservableCollection<Status>();
        ObservableCollection<User> UserCollection = new ObservableCollection<User>();

        public Search()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            Action fetchingAction;
            ShowProcessingMessage("正在搜索");

            string Keyword = txtSearchKeyword.Text;
            if (cmbSearchType.SelectedItem == cmItemStatus)
            {
                fetchingAction = () =>
                {
                    try
                    {
                        SearchedStatuses tempStatus = SearchBL.GetFirstPageSearchedStatus(Keyword);
                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            StatusCollection.Clear();
                            tempStatus.StatusList.ForEach(status => StatusCollection.Add(status));
                        }));
                        SetPagerEnabled();
                    }
                    catch
                    {
                        ShowFailedMessage("搜索失败,请重试");
                    }
                };
                lstSearchResult.ItemTemplate = (DataTemplate)this.FindResource("StatusItemTemplate");
                lstSearchResult.ItemsSource = StatusCollection;
            }
            else
            {
                fetchingAction = () =>
                {
                    try
                    {
                        List<User> tempUserList = SearchBL.GetFirstPageUser(Keyword);
                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            UserCollection.Clear();
                            tempUserList.ForEach(user => UserCollection.Add(user));
                        }));
                        SetPagerEnabled();
                    }
                    catch
                    {
                        ShowFailedMessage("搜索失败,请重试");
                    }
                };
                lstSearchResult.ItemTemplate = (DataTemplate)this.FindResource("UserItemsTemplate");
                lstSearchResult.ItemsSource = UserCollection;
            }
            fetchingAction.BeginInvoke(null, null);
        }
          

       
        private void SetPagerEnabled()
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                pager.CanPreviousePageClick =  SearchBL.currentPage > 1;
                pager.CanNextPageClick = !SearchBL.IsLastPage;
                base.HideMessage();
                //ShowSuccessfulMessage("获取成功");
            }
            ));
        }
         
        private void txtSearchKeyword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnSearch_Click(sender, e);
            }
        }

        public override void DoWork(object obj = null)
        {
            throw new NotImplementedException();
        }

        protected override void NotifyMainWindowNewMessageCountChanged(int messageCount)
        {
            throw new NotImplementedException();
        }

        public override void BeginInitializeMessage()
        {
            throw new NotImplementedException();
        }

        public override void pager_OnFirstPageClick(object sender, RoutedEventArgs e)
        {
            btnSearch_Click(sender, e);
        }

        public override void pager_OnPreviousePage(object sender, RoutedEventArgs e)
        {
            ShowProcessingMessage("正在搜索");
            if (cmbSearchType.SelectedItem == cmItemStatus)
            {
                Action fetchingAction = () =>
                {
                    try
                    {
                        SearchedStatuses tempStatus = SearchBL.GetPreviousePageSearchedStatus();
                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            StatusCollection.Clear();
                            tempStatus.StatusList.ForEach(status => StatusCollection.Add(status));

                            if (StatusCollection.Count > 0)
                            {
                                lstSearchResult.ScrollIntoView(StatusCollection[0]);
                            }
                        }));
                        SetPagerEnabled();
                    }
                    catch
                    {
                        ShowFailedMessage("搜索失败,请重试");
                    }

                };
                fetchingAction.BeginInvoke(null, null);
            }
            else
            {
                Action fetchingAction = () =>
                {
                    try
                    {
                        List<User> tempUsers = SearchBL.GetPreviousePageSearchedUser();
                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            UserCollection.Clear();
                            //tbSearchResultCount.Text = "共" + tempStatus.CurrentTotalCount + "条结果";
                            tempUsers.ForEach(user => UserCollection.Add(user));
                            if (StatusCollection.Count > 0)
                            {
                                lstSearchResult.ScrollIntoView(StatusCollection[0]);
                            }
                        }));
                        SetPagerEnabled();
                    }
                    catch
                    {
                        ShowFailedMessage("搜索失败,请重试");
                    }
                };
                fetchingAction.BeginInvoke(null, null);
            }
        }

        public override void pager_OnNextPageClick(object sender, RoutedEventArgs e)
        {
            if (cmbSearchType.SelectedItem == cmItemStatus)
            {
                ShowProcessingMessage("正在搜索");
                Action fetchingAction = () =>
                {
                    try
                    {
                        SearchedStatuses tempStatus = SearchBL.GetNextPageSearchedStatus();
                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            StatusCollection.Clear();
                            tempStatus.StatusList.ForEach(status => StatusCollection.Add(status));

                            if (StatusCollection.Count > 0)
                            {
                                lstSearchResult.ScrollIntoView(StatusCollection[0]);
                            }
                        })); 
                        SetPagerEnabled();
                    }
                    catch
                    {
                        ShowFailedMessage("搜索失败,请重试");
                    }
                };
                fetchingAction.BeginInvoke(null, null);
            }
            else
            {
                Action fetchingAction = () =>
                {
                    try
                    {
                        List<User> tempUsers = SearchBL.GetNextPageSearchedUser();
                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            UserCollection.Clear();
                            tempUsers.ForEach(user => UserCollection.Add(user));
                            if (StatusCollection.Count > 0)
                            {
                                lstSearchResult.ScrollIntoView(StatusCollection[0]);
                            }
                        }));
                        SetPagerEnabled();
                    }
                    catch
                    {
                        ShowFailedMessage("搜索失败,请重试");
                    }
                };
                fetchingAction.BeginInvoke(null, null);
            }
        }

        public override void StopFetchingMessage()
        {
            UserCollection.Clear();
            StatusCollection.Clear();
        }
    }
}
